﻿using System.Collections.Generic;
using StyleCop;

namespace Jsl.StyleCop.CSharp.UnitTesting
{
    /// <summary>
    /// A base class for all the StyleCop tests.
    /// </summary>
    /// <typeparam name="TRule">The type of the rule.</typeparam>
    public abstract class TestBase<TRule>
        where TRule : SourceAnalyzer
    {
        private readonly string ruleName;

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="TestBase&lt;TRule&gt;"/> class.
        /// </summary>
        /// <param name="ruleName">The identifier for the rule.</param>
        protected TestBase(string ruleName)
        {
            this.ruleName = ruleName;
        }
        #endregion Constructors

        #region Protected Methods
        /// <summary>
        /// Tests the method.
        /// </summary>
        /// <param name="methodCode">The method source code.</param>
        /// <returns>The violations, if any.</returns>
        protected IList<Violation> TestMethod(string methodCode)
        {
            string sourceCode = "namespace MyNamespace { public class Class { " + methodCode + " } }";
            return this.TestSource(sourceCode);
        }

        /// <summary>
        /// Tests the specified source code.
        /// </summary>
        /// <param name="sourceCode">The source code.</param>
        /// <returns>The violations, if any.</returns>
        protected IList<Violation> TestSource(string sourceCode)
        {
            return AnalysisHelper.Test(typeof(TRule), this.ruleName, sourceCode);
        }
        #endregion Protected Methods
    }
}